package com.portal.gui.marketman;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.portal.logic.ConnectionManager;
import com.portal.logic.Field;
import com.portal.logic.FieldDAO;
import com.portal.logic.SerializationUtil;
import com.portal.logic.TypeDAO;
import java.awt.Font;

public class EditTypeScr extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	//JComboBox cmbFields = new JComboBox();
	String selectedField = null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EditTypeScr frame = new EditTypeScr();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EditTypeScr() {
		setTitle("Edit Type");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblChooseTypeYou = new JLabel("Choose Type you want to edit:");
		lblChooseTypeYou.setBounds(22, 105, 194, 37);
		contentPane.add(lblChooseTypeYou);
		
		
		Connection connection = ConnectionManager.getConnection();
		final TypeDAO typeDAO = new TypeDAO(connection) ;
		List<com.portal.logic.Type> types = typeDAO.selectAll();
		
		final JComboBox cmbTypes = new JComboBox();
		Iterator<com.portal.logic.Type> iterator = types.iterator();
		while (iterator.hasNext()) {
			com.portal.logic.Type type = (com.portal.logic.Type) iterator.next();
			cmbTypes.addItem(type.getName());
		}
		
		cmbTypes.addItemListener(new ItemListener () {
		    public void actionPerformed(ActionEvent e) {
		    	//selectedField = e.getI;
		    	//cmbFields.setSelectedItem(selectedField);
		    }

			@Override
			public void itemStateChanged(ItemEvent itemevent) {
				selectedField = (String)itemevent.getItem();
				System.err.println(selectedField);
			}
		});
		
		cmbTypes.setBounds(250, 112, 156, 22);
		contentPane.add(cmbTypes);
		
		JLabel lblChooseOtherField = new JLabel("Choose Field you want give to Type:");
		lblChooseOtherField.setBounds(22, 68, 188, 37);
		contentPane.add(lblChooseOtherField);
		
		final FieldDAO fieldDAO = new FieldDAO(connection) ;
		List<Field> fields = fieldDAO.selectAll();
		
		final JComboBox cmbFields = new JComboBox();
		Iterator<Field> iterator2 = fields.iterator();
		while (iterator2.hasNext()) {
			Field field = (Field) iterator2.next();
			cmbFields.addItem(field.getName());
		}
		
		String selectedType = (String)cmbTypes.getSelectedItem();
		com.portal.logic.Type type = typeDAO.selectByName(selectedType);
		Field field = fieldDAO.selectById(type.getIdField());
		selectedField = (String)field.getName();
		cmbFields.setSelectedItem(field.getName());
		
		cmbFields.setBounds(250, 68, 156, 22);
		contentPane.add(cmbFields);
		
		JLabel lblNewNameFor = new JLabel("New Name for Type:");
		lblNewNameFor.setBounds(22, 153, 134, 16);
		contentPane.add(lblNewNameFor);
		
		textField = new JTextField();
		textField.setBounds(250, 145, 156, 22);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO handle Combobox value changing
				String selectedField = (String)cmbFields.getSelectedItem();
				String selectedType = (String)cmbTypes.getSelectedItem();
				String updatedTypeName = (String)textField.getText();
				
				Field field = fieldDAO.selectByName(selectedField);
				com.portal.logic.Type type = typeDAO.selectByName(selectedType);
				type.setIdField(field.getId());
				type.setName(updatedTypeName);
				
				try {
					SerializationUtil.serialize(type, "c:\\editType.out");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				}
				typeDAO.update();
				JOptionPane.showMessageDialog(null, "Type was updated!");
			}
		});
		btnSubmit.setBounds(309, 183, 97, 25);
		contentPane.add(btnSubmit);
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(22, 225, 97, 25);
		contentPane.add(btnBack);
		
		JLabel lblPleaseChooseThe = new JLabel("Please Choose the field and type you want to edit");
		lblPleaseChooseThe.setFont(new Font("Tahoma", Font.ITALIC, 11));
		lblPleaseChooseThe.setBounds(10, 31, 267, 26);
		contentPane.add(lblPleaseChooseThe);
	}
}
